using System;
using System.Text;
using System.Web;
using System.Collections;
using System.Text.RegularExpressions;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using Castle.MonoRail.Framework.Adapters;
using NHibernate;
using NHibernate.Expression;
using VS.Facade;
using VS.Model;
using VS.Commutil;


namespace VS.WebManage.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    public class ProductManageController : SmartDispatcherController
    {
        public void Index()
        {

        }

        #region Create
        public void Create()
        {
            PropertyBag["PTypes"] = PTypeSystem.Instance.GetPType(0, 0);
            string strTemplate = "<input class=noninput type=checkbox value={0} name=ptypes id={2} onclick=\"return selectFirst({3},{4});\"> <lable>{1}</lable>";
            StringBuilder sb = new StringBuilder();
            IList ftypes = PTypeSystem.Instance.GetPType(0, 0);
            int i = 1;
            foreach (VS_PType type in ftypes)
            {
                sb.AppendLine(string.Format(strTemplate, type.VS_PTypeID, type.Title, "f" + i, "'fdiv'", "'" + i + "'"));
                sb.AppendLine("<br />");
                IList templist = PTypeSystem.Instance.GetPType(1, type.VS_PTypeID);
                if (templist.Count > 0) sb.AppendLine("<span id=fdiv" + i + ">&nbsp;&nbsp;&nbsp;");
                foreach (VS_PType item in templist)
                {
                    sb.AppendLine(string.Format(strTemplate, item.VS_PTypeID, item.Title, "s" + i, "'f'", "'" + i + "'"));
                }
                if (templist.Count > 0) sb.AppendLine("</span><br />");
                i++;
            }
            PropertyBag["PTypes_checkbox"] = sb.ToString();
        }

        public void CreateVS_Product([DataBind("VS_Product")]VS_Product product, string[] ptypes, HttpPostedFile ProPicFile)
        {
            ArrayList typelist = new ArrayList();
            foreach (string id in ptypes)
            {
                VS_PType type = VS_PType.Find(int.Parse(id));
                typelist.Add(type);
            }
            VS_Product pro = new VS_Product();
            pro.Content = product.Content;
            pro.CreateTime = DateTime.Now;
            pro.Hits = 0;
            pro.IsExcellent = product.IsExcellent;
            pro.IsNew = product.IsNew;
            pro.Original_Price = product.Original_Price;
            pro.Price = product.Price;
            pro.ProName = product.ProName;
            pro.Reserve = product.Reserve;
            pro.Specification = product.Specification;
            pro.Status = 1;
            pro.VS_PTypes = typelist;
            if (ProPicFile.ContentLength > 0)
            {
                string basePath = System.Web.HttpContext.Current.Server.MapPath("../Content/Upload/ProImg/");
                string DirName = WebUtil.FormatDate2DirectoryName();
                string FileName = WebUtil.Make_FileName();
                string FullPath = basePath + DirName;
                WebUtil.Create_Directory(FullPath);
                ThumbImg img = new ThumbImg(ProPicFile.InputStream);
                img.GetReducedImage(300, 300, FullPath + "\\" + FileName + "_b.jpg");
                img.GetReducedImage(120, 120, FullPath + "\\" + FileName + "_s.jpg");
                pro.ProPic = DirName + "/" + FileName;

            }
            pro.Create();
            Redirect("/ProductManage/create.vk");
        }
        #endregion

        public void List(string id)
        {
            PropertyBag["sUrl"] = Request.RawUrl;
            PropertyBag["currType"] = VS_PType.Find(int.Parse(id));
            PropertyBag["PTypes"] = PTypeSystem.Instance.GetPType(0, 0);

            VS_PType type = VS_PType.Find(int.Parse(id));
            IList Products = type.VS_Products;
            PropertyBag["id"] = id;
            PropertyBag["Products"] = PaginationHelper.CreatePagination(this, Products, 20);

        }

        public void DeleteVS_Product(string id, string sUrl)
        {
            VS_Product product = VS_Product.Find(int.Parse(id));
            product.Delete();
            Redirect(sUrl);
        }

        #region Edit
        public void Edit(string id,string sUrl)
        {
            VS_Product product = VS_Product.Find(int.Parse(id));
            PropertyBag["VS_Product"] = product;

            PropertyBag["PTypes"] = PTypeSystem.Instance.GetPType(0, 0);
            string strTemplate = "<input class=noninput type=checkbox value={0} name=ptypes id={2} {5} onclick=\"return selectFirst({3},{4});\"> <lable>{1}</lable>";
            StringBuilder sb = new StringBuilder();
            IList ftypes = PTypeSystem.Instance.GetPType(0, 0);
            int i = 1;
            foreach (VS_PType type in ftypes)
            {
                bool t1 = false;
                foreach (VS_PType itype in product.VS_PTypes)
                {
                    if (itype.VS_PTypeID == type.VS_PTypeID) 
                        t1 = true;
                }
                if (t1)
                {
                    sb.AppendLine(string.Format(strTemplate, type.VS_PTypeID, type.Title, "f" + i, "'fdiv'", "'" + i + "'", "checked=true"));
                }
                else
                {
                    sb.AppendLine(string.Format(strTemplate, type.VS_PTypeID, type.Title, "f" + i, "'fdiv'", "'" + i + "'", ""));
                }
                sb.AppendLine("<br />");
                IList templist = PTypeSystem.Instance.GetPType(1, type.VS_PTypeID);
                if (templist.Count > 0) sb.AppendLine("<span id=fdiv" + i + ">&nbsp;&nbsp;&nbsp;");
                foreach (VS_PType item in templist)
                {
                    bool t2 = false;
                    foreach (VS_PType itype in product.VS_PTypes)
                    {
                        if (itype.VS_PTypeID == item.VS_PTypeID)
                            t2 = true;
                    }
                    if (t2)
                    {
                        sb.AppendLine(string.Format(strTemplate, item.VS_PTypeID, item.Title, "s" + i, "'f'", "'" + i + "'", "checked=true"));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(strTemplate, item.VS_PTypeID, item.Title, "s" + i, "'f'", "'" + i + "'", ""));
                    }

                }
                if (templist.Count > 0) sb.AppendLine("</span><br />");
                i++;
            }
            PropertyBag["PTypes_checkbox"] = sb.ToString();
            PropertyBag["sUrl"] = sUrl;
            PropertyBag["id"] = id; 
        }

        public void EditVS_Product([DataBind("VS_Product")]VS_Product product, string[] ptypes, HttpPostedFile ProPicFile,string sUrl,string id)
        {
            ArrayList typelist = new ArrayList();
            foreach (string ptypeid in ptypes)
            {
                VS_PType type = VS_PType.Find(int.Parse(ptypeid));
                typelist.Add(type);
            }
            VS_Product pro = VS_Product.Find(int.Parse(id));
            pro.Content = product.Content;
            pro.CreateTime = DateTime.Now;
            pro.IsExcellent = product.IsExcellent;
            pro.IsNew = product.IsNew;
            pro.Original_Price = product.Original_Price;
            pro.Price = product.Price;
            pro.ProName = product.ProName;
            pro.Reserve = product.Reserve;
            pro.Specification = product.Specification;
            pro.VS_PTypes = typelist;
            if (ProPicFile.ContentLength > 0)
            {
                string basePath = System.Web.HttpContext.Current.Server.MapPath("../Content/Upload/ProImg/");
                string DirName = WebUtil.FormatDate2DirectoryName();
                string FileName = WebUtil.Make_FileName();
                string FullPath = basePath + DirName;
                WebUtil.Create_Directory(FullPath);
                ThumbImg img = new ThumbImg(ProPicFile.InputStream);
                img.GetReducedImage(300, 300, FullPath + "\\" + FileName + "_b.jpg");
                img.GetReducedImage(120, 120, FullPath + "\\" + FileName + "_s.jpg");
                pro.ProPic = DirName + "/" + FileName;

            }
            pro.Update();
            Redirect(sUrl);
        }
        #endregion
    }
}